﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.LogEntries.BattleStartedLogEntry
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using Helpers;
using System.Collections.Generic;
using TaleWorlds.CampaignSystem.Party;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.Core;
using TaleWorlds.Localization;
using TaleWorlds.ObjectSystem;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.LogEntries
{
  public class BattleStartedLogEntry : LogEntry, IEncyclopediaLog, IChatNotification
  {
    [SaveableField(40)]
    private readonly uint _notificationTextColor;
    [SaveableField(41)]
    private readonly Settlement _settlement;
    [SaveableField(42)]
    private readonly TextObject _attackerName;
    [SaveableField(43)]
    private readonly TextObject _defenderName;
    [SaveableField(44)]
    private readonly bool _isVisibleNotification;
    [SaveableField(45)]
    private readonly uint _battleDetail;
    [SaveableField(46)]
    private readonly IFaction _attackerFaction;
    [SaveableField(47)]
    private readonly Hero _attackerLord;
    [SaveableField(48)]
    private readonly bool _attackerPartyHasArmy;

    internal static void AutoGeneratedStaticCollectObjectsBattleStartedLogEntry(
      object o,
      List<object> collectedObjects)
    {
      ((LogEntry) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
      collectedObjects.Add((object) this._settlement);
      collectedObjects.Add((object) this._attackerName);
      collectedObjects.Add((object) this._defenderName);
      collectedObjects.Add((object) this._attackerFaction);
      collectedObjects.Add((object) this._attackerLord);
    }

    internal static object AutoGeneratedGetMemberValue_notificationTextColor(object o)
    {
      return (object) ((BattleStartedLogEntry) o)._notificationTextColor;
    }

    internal static object AutoGeneratedGetMemberValue_settlement(object o)
    {
      return (object) ((BattleStartedLogEntry) o)._settlement;
    }

    internal static object AutoGeneratedGetMemberValue_attackerName(object o)
    {
      return (object) ((BattleStartedLogEntry) o)._attackerName;
    }

    internal static object AutoGeneratedGetMemberValue_defenderName(object o)
    {
      return (object) ((BattleStartedLogEntry) o)._defenderName;
    }

    internal static object AutoGeneratedGetMemberValue_isVisibleNotification(object o)
    {
      return (object) ((BattleStartedLogEntry) o)._isVisibleNotification;
    }

    internal static object AutoGeneratedGetMemberValue_battleDetail(object o)
    {
      return (object) ((BattleStartedLogEntry) o)._battleDetail;
    }

    internal static object AutoGeneratedGetMemberValue_attackerFaction(object o)
    {
      return (object) ((BattleStartedLogEntry) o)._attackerFaction;
    }

    internal static object AutoGeneratedGetMemberValue_attackerLord(object o)
    {
      return (object) ((BattleStartedLogEntry) o)._attackerLord;
    }

    internal static object AutoGeneratedGetMemberValue_attackerPartyHasArmy(object o)
    {
      return (object) ((BattleStartedLogEntry) o)._attackerPartyHasArmy;
    }

    public bool IsVisibleNotification => this._isVisibleNotification;

    public override ChatNotificationType NotificationType
    {
      get => this.AdversityNotification((IFaction) null, (IFaction) null);
    }

    public BattleStartedLogEntry(PartyBase attackerParty, PartyBase defenderParty, object subject)
    {
      this._notificationTextColor = defenderParty.MapFaction.LabelColor;
      this._attackerName = !attackerParty.IsMobile || attackerParty.MobileParty.Army == null ? attackerParty.Name : attackerParty.MobileParty.ArmyName;
      this._defenderName = !defenderParty.IsMobile || defenderParty.MobileParty.Army == null ? defenderParty.Name : defenderParty.MobileParty.ArmyName;
      this._attackerFaction = attackerParty.MapFaction;
      this._attackerPartyHasArmy = attackerParty.IsMobile && attackerParty.MobileParty.Army != null && attackerParty.MobileParty.LeaderHero != null;
      if (attackerParty.IsMobile && attackerParty.MobileParty.LeaderHero != null)
        this._attackerLord = attackerParty.MobileParty.LeaderHero;
      this._settlement = subject as Settlement;
      if (this._settlement != null)
      {
        this._isVisibleNotification = true;
        if (this._settlement.IsVillage)
        {
          this._battleDetail = this._settlement.MapFaction.IsAtWarWith(attackerParty.MapFaction) ? 1U : 0U;
        }
        else
        {
          if (!this._settlement.IsFortification)
            return;
          this._battleDetail = this._settlement.MapFaction.IsAtWarWith(attackerParty.MapFaction) ? 3U : 2U;
        }
      }
      else
      {
        if (!(subject is Army))
          return;
        this._battleDetail = 4U;
      }
    }

    public override string ToString() => this.GetEncyclopediaText().ToString();

    public TextObject GetNotificationText() => this.GetEncyclopediaText();

    public bool IsVisibleInEncyclopediaPageOf<T>(T obj) where T : MBObjectBase
    {
      return (object) obj == this._settlement;
    }

    public TextObject GetEncyclopediaText()
    {
      TextObject parent = TextObject.Empty;
      TextObject variable = this._settlement != null ? this._settlement.EncyclopediaLinkWithName : TextObject.Empty;
      switch (this._battleDetail)
      {
        case 0:
          parent = GameTexts.FindText("str_army_defend_from_raiding_news");
          parent.SetTextVariable("PARTY", this._attackerName);
          parent.SetTextVariable("SETTLEMENT", variable);
          break;
        case 1:
          if (this._attackerFaction == null || this._attackerLord == null)
          {
            parent = new TextObject("{=xss7eP0f}{PARTY} is raiding {SETTLEMENT}");
            parent.SetTextVariable("PARTY", this._attackerName);
            parent.SetTextVariable("SETTLEMENT", variable);
            break;
          }
          parent = !this._attackerPartyHasArmy ? GameTexts.FindText("str_party_raiding_news") : GameTexts.FindText("str_army_raiding_news");
          StringHelpers.SetCharacterProperties("LORD", this._attackerLord.CharacterObject, parent);
          parent.SetTextVariable("FACTION_NAME", this._attackerFaction.EncyclopediaLinkWithName);
          parent.SetTextVariable("SETTLEMENT", variable);
          break;
        case 2:
          parent = GameTexts.FindText("str_defend_from_assault_news");
          parent.SetTextVariable("PARTY", this._attackerName);
          parent.SetTextVariable("SETTLEMENT", variable);
          break;
        case 3:
          parent = GameTexts.FindText("str_assault_news");
          parent.SetTextVariable("PARTY", this._attackerName);
          parent.SetTextVariable("SETTLEMENT", variable);
          break;
        case 4:
          parent = GameTexts.FindText("str_army_battle_news");
          parent.SetTextVariable("PARTY1", this._attackerName);
          parent.SetTextVariable("PARTY2", this._defenderName);
          break;
      }
      return parent;
    }

    private enum BattleDetail
    {
      AttackerPartyDefendingVillage,
      AttackerPartyRaidingVillage,
      AttackerPartyDefendingFortification,
      AttackerPartyBesiegingFortification,
      ArmyBattle,
    }
  }
}
